/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.table;

import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;

public class BooleanTableCellRenderer extends JCheckBox 
		implements AdvancedTableCellRenderer {
	private static final long serialVersionUID = -3598806221749938693L;
	
	private int rowToHighlight = -1;
	private int columnToHighlight = -1;
	private boolean rowHighlighted = true;
	private boolean columnHighlighted = false;
	private Color highlightColor = DEFAULT_HIGHLIGHT_COLOR;
	private Color highlightForegroundColor = null;
	private Color stripesColor = DEFAULT_STRIPES_COLOR;
	
	private Border focusBorder;
	private Border noFocusBorder;
	
	public BooleanTableCellRenderer() {
		super();
		setHorizontalAlignment(JCheckBox.CENTER);
		setBorderPainted(true);
	}
	
	@Override
	public void updateUI() {
		super.updateUI();
		focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
		Insets i = focusBorder.getBorderInsets(this);
		noFocusBorder = BorderFactory.createEmptyBorder(i.top,
				i.left, 
				i.bottom, 
				i.right);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		
		setSelected(Boolean.TRUE.equals(value));
		
		if (isSelected) {
			setBackground(table.getSelectionBackground());
			setForeground(table.getSelectionForeground());
		} else if (row % 2 == 1) {
			setBackground(getStripes());
			setForeground(table.getForeground());
		} else {
			setBackground(table.getBackground());
			setForeground(table.getForeground());
		}
		
		if(hasFocus) {
			setBorder(focusBorder);
		} else {
			setBorder(noFocusBorder);
		}
		
		boolean hColumn = column == getHighlightedColumn() && 
				getHighlightedColumn() >= 0 && isColumnHighlightAllowed();
		boolean hRow = row == getHighlightedRow() && 
				getHighlightedRow() >= 0 && isRowHighlightAllowed();
		
		if(hColumn || hRow) {
			if(getHighlightForeground() != null) {
				setForeground(getForeground());
			}
			setBackground(getHighlightBackground());
		}
		
		return this;
	}
	
	@Override
	public void setHighlightedRow(int row) {
		rowToHighlight = row;
	}
	@Override
	public int getHighlightedRow() {
		return rowToHighlight;
	}
	
	@Override
	public void setHighlightedColumn(int column) {
		columnToHighlight = column;
	}
	@Override
	public int getHighlightedColumn() {
		return columnToHighlight;
	}
	
	@Override
	public void setHighlightBackground(Color highlightColor) {
		this.highlightColor = highlightColor;
	}
	@Override
	public Color getHighlightBackground() {
		return highlightColor;
	}
	
	@Override
	public Color getHighlightForeground() {
		return highlightForegroundColor;
	}
	@Override
	public void setHighlightForeground(Color highlightColor) {
		highlightForegroundColor = highlightColor;
	}
	
	@Override
	public void setRowHighlightAllow(boolean rowHighlighted) {
		this.rowHighlighted = rowHighlighted;
	}
	@Override
	public boolean isRowHighlightAllowed() {
		return rowHighlighted;
	}
	
	@Override
	public void setColumnHighlightAllowed(boolean columnHighlighted) {
		this.columnHighlighted = columnHighlighted;
	}
	@Override
	public boolean isColumnHighlightAllowed() {
		return columnHighlighted;
	}
	
	@Override
	public void setStripes(Color stripesColor) {
		this.stripesColor = stripesColor;
	}
	@Override
	public Color getStripes() {
		return stripesColor;
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {
		mouseMoved(e);
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		if(!(e.getComponent() instanceof JTable)) {
			return;
		}
		
		boolean doRepain = getHighlightedRow() != -1;
		setHighlightedRow(-1);
		setHighlightedColumn(-1);
		if(doRepain) {
			((JTable)e.getComponent()).repaint();
		}
	}
	
	@Override
	public void mouseMoved(MouseEvent e) {
		if(!(e.getComponent() instanceof JTable)) {
			return;
		}
		
		JTable table = (JTable) e.getComponent();
		
		int mouseOverRow = table.rowAtPoint(e.getPoint());
		int mouseOverColumn = table.columnAtPoint(e.getPoint());
		
		boolean doRepain = (getHighlightedColumn() != mouseOverColumn) || 
				(getHighlightedRow() != mouseOverRow);
		setHighlightedRow(mouseOverRow);
		setHighlightedColumn(mouseOverColumn);
		if(doRepain) {
			table.repaint();
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
	}
	@Override
	public void mousePressed(MouseEvent e) {
	}
	@Override
	public void mouseReleased(MouseEvent e) {
	}
	@Override
	public void mouseDragged(MouseEvent e) {
	}
}
